package com.suredy.app.porject.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.app.porject.constants.UserFileType;
import com.suredy.app.porject.model.Bid;
import com.suredy.app.porject.model.BidFile;
import com.suredy.core.service.BaseSrvWithEntity;

@Service
public class BidFileSrv extends BaseSrvWithEntity<BidFile> {

	public BidFileSrv() {
		this.addAnDefOrder("fileName", true);
	}

	@Override
	public DetachedCriteria getDc(BidFile t) {
		DetachedCriteria dc = super.getDc(t);

		if (t == null)
			return dc;

		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		if (StringUtils.isBlank(t.getSuffix())) {
			dc.add(Restrictions.eq("suffix", t.getSuffix()));
		}
		if (StringUtils.isBlank(t.getUserName())) {
			dc.add(Restrictions.eq("userName", t.getUserName()));
		}
		if (t.getBid() != null) {
			dc.add(Restrictions.eq("bid", t.getBid()));
		}

		return dc;
	}

	/**
	 * 指定文件是否存在
	 * 
	 * @param bidId
	 * @param type
	 * @return
	 */
	public boolean isFileExists(String bidId, UserFileType type) {
		if (StringUtils.isBlank(bidId))
			return false;

		if (type == null)
			return false;

		String ql = "SELECT EXISTS(SELECT T FROM BidFile T WHERE T.bid=? AND T.type=?)";

		Bid b = new Bid();
		b.setId(bidId);

		int i = (int) this.readSingleByQL(ql, b, type);

		return i != 0;
	}

}
